﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InternetSpiderLib
{
    /// <summary>
    /// 业务创建的接口
    /// </summary>
    public interface IBusinessCreater
    {
        Action Create();
    }

    /// <summary>
    /// 匿名的业务创建接口
    /// </summary>
    public class AnonymousBusinessCreater : IBusinessCreater
    {
        public Func<Action> CreaterFunc { get; set; }

        public AnonymousBusinessCreater() { }

        public AnonymousBusinessCreater(Func<Action> func)
        {
            CreaterFunc = func;
        }

        public Action Create()
        {
            return CreaterFunc?.Invoke();
        }
    }

    /// <summary>
    /// 顺序业务创建器
    /// </summary>
    public class OrderBusinessCreater : IBusinessCreater
    {
        private int Index = 0;

        private List<IBusinessCreater> _Creates = new List<IBusinessCreater>();


        public OrderBusinessCreater(List<IBusinessCreater> creaters)
        {
            _Creates.AddRange(creaters);
        }

        public Action Create()
        {
            if (_Creates.Count == 0)
                return null;

            Action action = null;
            int start_index = Index;
            while (true)
            {
                var current = _Creates[Index];
                action = current.Create();
                if (action != null)
                    break;

                Index = (Index + 1) % _Creates.Count;

                if (start_index == Index)
                    break;
            }

            return action;
        }
    }

    /// <summary>
    /// 从各个模块中随机创建实例
    /// </summary>
    public class RandomBusinessCreater : IBusinessCreater
    {
        private List<IBusinessCreater> _Creaters = new List<IBusinessCreater>();

        public RandomBusinessCreater(List<IBusinessCreater> creaters)
        {
            _Creaters.AddRange(creaters);
        }

        public Action Create()
        {
            Random random = new Random((int)DateTime.Now.Ticks);

            Action action = null;
            List<KeyValuePair<int, IBusinessCreater>> temp = new List<KeyValuePair<int, IBusinessCreater>>();
            temp.AddRange(_Creaters.Select(n => new KeyValuePair<int, IBusinessCreater>(random.Next(), n)));

            temp = temp.OrderBy(n => n.Key).ToList();

            foreach (var item in temp)
            {
                action = item.Value.Create();
                if (action != null)
                    break;
            }

            return action;
        }
    }

    /// <summary>
    /// 选择业务创建器
    /// （依次尝试从不同的业务中创建业务）
    /// </summary>
    public class SelectBusinessCreater : IBusinessCreater
    {
        private List<IBusinessCreater> _Creaters = new List<IBusinessCreater>();

        public SelectBusinessCreater(List<IBusinessCreater> creaters)
        {
            _Creaters.AddRange(creaters);
        }

        /// <summary>
        /// 创建活动
        /// </summary>
        /// <returns></returns>
        public Action Create()
        {
            Action action = null;
            foreach (var item in _Creaters)
            {
                action = item.Create();
                if (action != null)
                    break;
            }
            return action;
        }
    }
}
